<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../../assets/img/home/heima.png" alt="">
        <span>电商管理后台系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
<!--          一级菜单-->
          <el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id">
<!--            一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
<!--            二级菜单-->
            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧区域 -->
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [], // 左侧菜单数据保存
      iconsObj: { // 定义不同的图标的做法
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false, // 是否折叠侧边菜单栏
      activePath: '' // 定义保存的被激活链接地址path的activePath为一个空字符串
    }
  },
  created () {
    this.getMenuList()
    // 一旦主页组件创建成功，就将sessionStorage中保存的被激活的链接地址赋给activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录操作
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据操作
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
      // console.log(res)
    },
    // 点击按钮切换侧边菜单的折叠和展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 将二级菜单的path保存到sessionStorage中，也就是保存菜单的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      // 解决点击其他菜单，会出现没有高亮的状态
      this.activePath = activePath
    }
  }
}
</script>

<style scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373041;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #Ffffff;
  font-size: 20px;
}
.el-header div{
  display: flex;
  align-items: center;
}
.el-header div span{
  margin-left: 15px;
}
.el-aside{
  background-color: #333744;
}
.el-aside .el-menu{
  border-right: none;
}
.el-main{
  background-color: #eaeaf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #Ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
